const schema = {
  namespaced: true,
  state: () => ({
    sss: {
      root: {
        type: "object",
        properties: {
          form: {
            type: "void",
            "x-component": "FormGrid",
            "x-component-props": { minColumns: [2], maxColumns: [2] },
            "x-decorator": "Form",
            "x-decorator-props": {
              colon: false,
              labelWidth: 110,
            },
            properties: {
              code: {
                type: "string",
                title: "任务编号",
                required: true,
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "Input",
                "x-component-props": {
                  disabled: true,
                  size: "medium",
                  placeholder: "请输入",
                },
              },
              title: {
                type: "string",
                title: "任务名称",
                required: true,
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "Input",
                "x-component-props": {
                  size: "medium",
                  clearable: true,
                  placeholder: "{{vueThis.$t('common.please_input')}}",
                },
              },
              level: {
                type: "string",
                title: "优先级",
                enum: [],
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "Select",
                "x-component-props": {
                  size: "medium",
                  clearable: true,
                  placeholder: "请选择",
                },
                "x-reactions": ["{{getDictionarys}}"],
                "f-definition": {
                  "getDictionarys-field": `field.loading = true; 
                        let titles = "任务优先级";
                        this.$server()
                          .batchQueryDictionary(titles)
                          .then(
                            this.action.bound((data) => {
                              let items = data[0].details.map((item) => {
                                return {
                                  value: item.k,
                                  label: item.v,
                                };
                              });
                              field.dataSource = items;
                              field.loading = false;
                            })
                          )
                          .catch((response) => {
                            this.$message.error(response.data.message);
                          });
                        `,
                },
              },
              responsibleId: {
                type: "string",
                title: "负责人",
                enum: [],
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "Select",
                "x-component-props": {
                  size: "medium",
                  clearable: true,
                  placeholder: "请选择",
                  "@change": "{{handlePersonChange}}",
                },
                "x-reactions": ["{{getPersonList}}"],
                "f-definition": {
                  "getPersonList-field": `field.loading = true;
                        this.$server()
                        .query("function-user", "DepartmentUser", {}, null, null, false)
                        .then(
                          this.action.bound((data) => {
                            let userItem = [];
                            data.list.forEach((element) => {
                              if (element.data && element.department) {
                                userItem.push({
                                  value: element.data.id.toString(),
                                  label: element.data.name + '(' + element.department.title + ')',
                                  name: element.data.name,
                                  department: element.department.title,
                                });
                              }
                            });
                            field.dataSource = userItem.filter((item) => item.name !== null);
                            field.loading = false
                          })  
                        )
                        .catch((err) => {
                          this.$message.error(err.data.message);
                        });
                      `,
                  handlePersonChange: `const ele = arguments[0];
                        this.form.getFieldState('responsibleId',(state) => {
                          let item = state.dataSource.find((item) => item.value == ele);
                          console.log(item);
                          let obj = {
                            type: "user",
                            responsible: 1,
                            sourseId: item.value,
                            sourseName: item.name,
                          };
                          this.form.values.responsibleUser = obj;
                          this.form.values.taskSourseDTOList = [];
                          if (this.form.values.taskSourseDTOList && this.form.values.taskSourseDTOList.length > 0) {
                            this.form.values.taskSourseDTOList = this.form.taskSourseDTOList.filter((item) => item.type !== "用户");
                          }
                          this.form.values.taskSourseDTOList.push(obj);
                        })`,
                },
              },
              planBegin: {
                type: "string",
                title: "计划开始时间",
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "DatePicker",
                "x-component-props": {
                  size: "medium",
                  clearable: true,
                  type: "datetime",
                  format: "yyyy-MM-dd HH:mm",
                  placeholder: "请选择",
                  "@change": "{{vueThis.calculateWorkTime}}",
                },
                "x-reactions": {
                  target: "planEnd",
                  effects: ["onFieldValueChange"],
                  fulfill: {
                    run: `$form.setFieldState("planEnd", (state) => {
                          state.setComponentProps({
                            pickerOptions: {
                              disabledDate: (time) => {
                                if ($self.value) {
                                  return time.getTime() <= new Date($self.value).getTime();
                                } else {
                                  return false;
                                }
                              },
                            },
                          });
                        })`,
                  },
                },
                "f-definition": {
                  calculateWorkTime: `const value = arguments[0];
                      if (value == null) {
                        this.form.values.workTime = null;
                      }
                      if (this.form.values.planBegin && this.form.values.planEnd) {
                        const workTime = this.util.timeDiff(this.form.values.planBegin, this.form.values.planEnd);
                        this.form.values.workTime = workTime;
                      }`,
                },
              },
              planEnd: {
                type: "string",
                title: "计划结束时间",
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "DatePicker",
                "x-component-props": {
                  size: "medium",
                  clearable: true,
                  type: "datetime",
                  format: "yyyy-MM-dd HH:mm",
                  placeholder: "请选择",
                  "@change": "{{calculateWorkTime}}",
                },
                "x-reactions": {
                  target: "planBegin",
                  effects: ["onFieldValueChange"],
                  fulfill: {
                    run: `$form.setFieldState("planBegin", (state) => {
                            state.setComponentProps({
                              pickerOptions: {
                                disabledDate: (time) => {
                                  if ($self.value) {
                                    return time.getTime() >= new Date($self.value).getTime();
                                  } else {
                                    return false;
                                  }
                                },
                              },
                            });
                          })`,
                  },
                },
              },
              workTime: {
                type: "string",
                title: "计划工期",
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "Input",
                "x-component-props": {
                  disabled: true,
                  size: "medium",
                },
              },
              point: {
                type: "string",
                title: "生产进度",
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "Input",
                "x-component-props": {
                  disabled: true,
                  size: "medium",
                },
                "x-content": {
                  append: {
                    render: "{{appendRender}}",
                  },
                },
                "f-definition": {
                  appendRender: `const h = arguments[0];return h("span",null, ["%"]);`,
                },
              },
              beginTime: {
                type: "string",
                title: "开始时间",
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "DatePicker",
                "x-component-props": {
                  size: "medium",
                  clearable: true,
                  type: "datetime",
                  format: "yyyy-MM-dd HH:mm",
                  placeholder: "请选择",
                },
                "x-reactions": {
                  target: "endTime",
                  effects: ["onFieldValueChange"],
                  fulfill: {
                    run: `$form.setFieldState("endTime", (state) => {
                          state.setComponentProps({
                            pickerOptions: {
                              disabledDate: (time) => {
                                if ($self.value) {
                                  return time.getTime() <= new Date($self.value).getTime();
                                } else {
                                  return false;
                                }
                              },
                            },
                          });
                        })`,
                  },
                },
              },
              endTime: {
                type: "string",
                title: "结束时间",
                "x-decorator": "FormItem",
                "x-decorator-props": {},
                "x-component": "DatePicker",
                "x-component-props": {
                  size: "medium",
                  clearable: true,
                  type: "datetime",
                  format: "yyyy-MM-dd HH:mm",
                  placeholder: "请选择",
                },
                "x-reactions": {
                  target: "beginTime",
                  effects: ["onFieldValueChange"],
                  fulfill: {
                    run: `$form.setFieldState("beginTime", (state) => {
                          state.setComponentProps({
                            pickerOptions: {
                              disabledDate: (time) => {
                                if ($self.value) {
                                  return time.getTime() >= new Date($self.value).getTime();
                                } else {
                                  return false;
                                }
                              },
                            },
                          });
                        })`,
                  },
                },
              },
            },
          },
        },
      },
    },
  }),
  mutations: {
    UPDATE_SCHEMA(state, payload) {
      payload.forEach((schema) => {
        console.log("UPDATE_SCHEMA");
        let bytes = Uint8Array.from(atob(schema.data), (c) => c.charCodeAt(0));
        let decodedString = new TextDecoder("utf-8").decode(bytes);
        state[schema.code] = JSON.parse(decodedString);
        // state[schema.code] = JSON.parse(schema.data);
      });
    },
  },
  actions: {},
};
export default schema;
